package dao;

import bean.User;
import util.DBUtil;
import java.sql.*;

public class UserDAO {
    public void addUser(User user) {

        String sql = "INSERT INTO userInfo VALUES(?,?)";
        try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {

            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());

            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static User getUser(String username){
        String sql = "select * from userInfo where username = "+username;
        User user = new User();
        user.setUsername(null);
        user.setPassword(null);
        try (Connection c = DBUtil.getConnection(); Statement st = c.createStatement()) {
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()){
                user.setUsername(rs.getString(1));
                user.setPassword(rs.getString(2));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }
}
